<?php
/**
 * @deprecated (暂时没用)
 *  虚拟仓商品的管理
 * Author: lyun
 * Date: 2018/4/3
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Store\Product;

use App\Http\Middleware\CustomerAuth;
use App\Http\Requests\Admin\Customer\VirtualProductIndexRequest;
use App\Http\Requests\Admin\Customer\VirtualProductOpenRequest;
use App\Http\Requests\Admin\Customer\VirtualProductStoreRequest;
use App\Models\Customer\Customer;
use App\Models\Panx\Product;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class VirtualProductController extends Controller
{
    use ApiResponser,ApiChecker;

    /**
     * Display a listing of the resource.
     *
     * @param VirtualProductIndexRequest $request
     *
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(VirtualProductIndexRequest $request)
    {
        // 判断是否是虚拟客户
        if (CustomerAuth::$customer->provider_type === Customer::PROVIDER_TYPE_VIRTUAL) {
            $this->checkIsTrue(false, trans('admin.is_virtual_provider'));
        }

        // 代理是否开启了虚拟仓
        if (CustomerAuth::$customer->show_virtual === Customer::HIDE_VIRTUAL) {
            $this->checkIsTrue(false, trans('admin.is_show_virtual'));
        }

        // 所有的正常的虚拟客户
        $virtual_customer_ids = Customer::where('provider_type', Customer::PROVIDER_TYPE_VIRTUAL)
            ->where('is_deleted', Customer::IS_NOT_DELETED)
            ->pluck('id')
            ->all();

        // 显示可用虚拟仓的商品,除了虚拟客户
        $products = Product::whereIn('customer_id', $virtual_customer_ids)
            ->where('is_delete', Product::IS_NO_DELETE)
            ->where('is_sale', Product::IS_SALE)
            ->filterIsHot($request->is_hot)
            ->filterIsNew($request->is_new)
            ->paginate($request->paginate?:15);

        return $this->response(compact('products'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  VirtualProductStoreRequest $request
     *
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function store(VirtualProductStoreRequest $request)
    {
        // 判断是否是虚拟客户
        if (CustomerAuth::$customer->provider_type === Customer::PROVIDER_TYPE_VIRTUAL) {
            $this->checkIsTrue(false, trans('admin.is_virtual_provider'));
        }

        // 代理是否开启了虚拟仓
        if (CustomerAuth::$customer->show_virtual === Customer::HIDE_VIRTUAL) {
            $this->checkIsTrue(false, trans('admin.is_show_virtual'));
        }

        $product_ids = $request->product_ids;

        /** @var Customer $customer */
        $customer = CustomerAuth::$customer;

        $customer->virtualProducts()->sync($product_ids);

        return $this->response(compact('product_ids'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     *  开启和关闭虚拟仓
     * @param VirtualProductOpenRequest $request
     * @return array|\Illuminate\Http\Response
     */
    public function openVirtualProduct(VirtualProductOpenRequest $request)
    {
        $show_virtual = $request->show_virtual;

        /** @var Customer $customer */
        $customer = CustomerAuth::$customer;

        $updated = $customer->update(['show_virtual'=>$show_virtual]);

        return $this->response(compact('updated'));
    }
}
